package serverlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import until.Dofaq.Actionfaq;
import connectDB.FaqDB;
import connectDB.IDB;
import connectDB.PacketDB;
import entities.Faqentities;
import entities.Packetentities;

/**
 * Servlet implementation class Faqcontroller
 */
@WebServlet("/Faqcontroller")
public class Faqcontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("btaction");
		
		
		if(action.equals("delete")){
				this.doDelete(request, response);
		}		
			
			if(action.equals("edit"))   //edit
				{
					this.doEdit(request, response);
				}
			
		
			  else if(action.equals("add"))
				{
				this.doAdd(request, response);
				}
				
	}
	
	private void doEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			try {
				IDB<Faqentities, Boolean, String> acc = new FaqDB();
				int id = Integer.parseInt(request.getParameter("id"));
				String title = request.getParameter("title");
				String message = request.getParameter("message");
				Faqentities fa = new Faqentities(id, title, message);
				boolean result = acc.update(fa);
				String url = null;
				if(result){
					HttpSession session = request.getSession(true);
					url = "/Manager/faqlist.jsp";
				}
				RequestDispatcher rd = request.getRequestDispatcher(url);
				rd.forward(request, response);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {


		try {
			IDB<Faqentities, Boolean, String> acc = new FaqDB();
			int packetid = 0;
			String title = request.getParameter("title");
			String message = request.getParameter("message");
			//Packetentities land = new Packetentities(packetid, packetname);
			Faqentities land = new Faqentities(packetid, title, message);
			boolean result = acc.add(land);
			String url = null;
			if(result){
				HttpSession session = request.getSession(true);
				url = "/Manager/faqlist.jsp";
			}
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {


		try {	
			
			int x = Integer.parseInt(req.getParameter("id"));
			
			IDB<Faqentities,Boolean,String> acc = new FaqDB();
			
			boolean result = acc.delete1(x);
			String url = "/Manager/faqlist.jsp";
			RequestDispatcher dis = req.getRequestDispatcher(url);
			dis.forward(req, resp);
			
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
	}

}
